package logic;

import java.util.LinkedList;
import java.util.List;

import utils.CalculateRatio;
import utils.IdGenerator;

import data.IDestinationDAO;
import data.IProductDAO;
import data.IUserDAO;
import data.JDBCDestinationDAO;
import data.JDBCProductDAO;
import data.JDBCUserDAO;

public class Validate implements IValidate {

	
	private IDestinationDAO ddao = new JDBCDestinationDAO();
	private IUserDAO udao = new JDBCUserDAO();
	private IProductDAO pdao = new JDBCProductDAO();

	
	public void putDonatation(Product p, String idUser, String idDestination){
		
		Destination dest = ddao.select(idDestination);
		User usr = udao.select(idUser);
		usr.setAmountDonate(pdao.amountDonate(idUser));
		usr.setAmountDonateBuyed(pdao.amountDonatedPurchased(idUser));
		usr.setMoneyRaised(pdao.moneyRaised(idUser));
		CalculateRatio rate = new CalculateRatio();
		String idProduct = IdGenerator.getInstance().getKey();
		p.setId(idProduct);
		rate.calculateProduct(p, usr, dest);
		
		if(p!=null){
			System.out.println("antes de insertar producto");
			pdao.insert(p);
		}

	}
	
	public List<Product> getProductInactive(int i){
		List<Product> lp = new LinkedList<Product>();
		lp = pdao.selectNProductNotActive(i);
		if(lp == null){
			return null;
		}
		for(Product prod :lp){
			String idDest = prod.getIddestination();
			Destination dest = ddao.select(idDest);
			prod.setNameDestination(dest.getName());
			}
		
		return lp;
		
	}
	
	public void changeState(String idProd){
		Product p = pdao.selectProduct(idProd);
		p.setActive(true);
		pdao.update(p);
	}
	
	public void delete(String idProd){
		Product p = pdao.selectProduct(idProd);
		pdao.delete(p.getId());
	}

}